package com.qtyx.nms.ui.activity

import android.util.Log
import android.widget.FrameLayout
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.qtyx.nms.R
import com.qtyx.nms.ui.base.BaseActivity
import com.qtyx.nms.util.ConstantUtil
import com.qtyx.nms.util.shared.AppInfoShared
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.listener.OnPageChangeListener
import kotlinx.android.synthetic.main.ac_base.*
import kotlinx.android.synthetic.main.ac_skin_select.*
import skin.support.SkinCompatManager

/**
 * 皮肤选择
 */
class SkinSelectActivity : BaseActivity() {
    override fun getLayoutId()=R.layout.ac_skin_select

    lateinit var backLp:FrameLayout.LayoutParams

    override fun initData() {
        val images= listOf(
            R.drawable.ic_skin1,
            R.drawable.ic_skin2,
            R.drawable.ic_skin3,
        )

        val skinNames= listOf("经典黑","清新粉","活力橙")

        var currentPosition=AppInfoShared.getSkinType()
        tvSkinName.text=skinNames[currentPosition]+"（使用中）"

        bannerSkin.let {
            it.addBannerLifecycleObserver(this)
            it.setBannerGalleryEffect(150,150,20,0.7f)
            it.adapter = object : BannerImageAdapter<Int>(images){
                override fun onBindView(holder: BannerImageHolder,data: Int,position: Int,size: Int) {
                    holder.imageView.scaleType= ImageView.ScaleType.FIT_XY
                    Glide.with(mContext)
                        .load(data)
                        .skipMemoryCache(true)// 不使用内存缓存
                        .diskCacheStrategy(DiskCacheStrategy.NONE)// 不使用磁盘缓存
                        .into(holder.imageView)
                }
            }
            it.addOnPageChangeListener(object : OnPageChangeListener{
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {}
                override fun onPageScrollStateChanged(state: Int) {}
                override fun onPageSelected(position: Int) {
                    currentPosition=position
                    tvSkinName.text=skinNames[position]
                    if(position==AppInfoShared.getSkinType()){
                        tvSkinName.text=skinNames[position]+"（使用中）"
                    }
                }
            })

            it.setCurrentItem(currentPosition+1,false)
        }

        backLp=ivBaseBackButton.layoutParams as FrameLayout.LayoutParams
        backLp.leftMargin=300


        btnSkinSelected.setOnClickListener {
            AppInfoShared.saveSkinType(currentPosition)
            when(AppInfoShared.getSkinType()){
                0->{//默认主题
                    SkinCompatManager.getInstance().restoreDefaultTheme()
                }
                1->{//粉色
                    SkinCompatManager.getInstance().loadSkin(ConstantUtil.skinPinkPath, SkinCompatManager.SKIN_LOADER_STRATEGY_ASSETS)
                }
                2->{//橙色
                    SkinCompatManager.getInstance().loadSkin(ConstantUtil.orangePinkPath, SkinCompatManager.SKIN_LOADER_STRATEGY_ASSETS)
                }
            }
            tvSkinName.text=skinNames[currentPosition]+"（使用中）"
        }
    }




}